package com.xj.edu.hotel.controller;

import com.xj.edu.hotel.domain.Manager;
import com.xj.edu.hotel.domain.Role;
import com.xj.edu.hotel.service.MailService;
import com.xj.edu.hotel.service.ManagerService;

import com.xj.edu.hotel.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/manager")
//@SessionAttributes("manager")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MailService mailService;

    private String validateCheckCode;
    /**
     * 无认证管理员登录
     */
//    @RequestMapping("login")
//    public String login(Manager manager,Model model){
//       Manager managerY = managerService.findByName(manager);
//       if(managerY != null){
//          if (manager.getManagerPassword().equals(managerY.getManagerPassword())){
//              model.addAttribute("manager",managerY);
//              return "starter";
//          }else {
//              model.addAttribute("msg","密码错误");
//              return "login";
//          }
//       }else {
//           model.addAttribute("msg","用户不存在");
//           return "login";
//       }
//
//    }
    /**
     * 获取验证码
     */
    @RequestMapping("getCheckCode")
    @ResponseBody
    public String getCheckCode(String email){
        String checkCode = String.valueOf(new Random().nextInt(899999)+100000);
        String message = "您的验证码:"+checkCode;
        try {
            System.out.println("-------------"+checkCode);
            mailService.sendSimpleMail(email,"注册验证码",message);
            validateCheckCode = checkCode;
        }catch(Exception e){
            return "";
        }
        return checkCode;
    }
    /**
     * 管理员注册
     */
    @PostMapping(value = "register")
    @ResponseBody
    public String register(Manager manager, String checkCode, HttpSession session){
       List<Manager> list = managerService.findByName(manager.getManagerName());
       if(checkCode.equals(validateCheckCode)){
           if(list.size()>=1){
               return "用户已存在";
           }else {
               int success = managerService.insert(manager);
               if(success > 0){
                   return "ok";
               }else {
                   return "error";
               }
           }
       }else {
           return "验证码错误";
       }

    }



    /**
     * 管理员信息查询
     */

    @RequestMapping("findAllManager")
    public String findAllManager(Model model){
        List<Manager> managerList = managerService.findAll();
        model.addAttribute("managers",managerList);
        return "manager-role";
    }

    /**
     * 查找所有的角色类型
     */

    @RequestMapping("findAllRole")
    @ResponseBody
    public List<Role> findAllRole(){
        List<Role> list = roleService.findAllRoleType();
        return list;
    }
    /**
     * 删除管理员
     */

    @RequestMapping("delManager")
    public void delManager(String id){
       int success = managerService.delManager(id);
    }

    /**
     *通过id查询
     */

    @RequestMapping("findById")
    public String findById(String id,Model model){
        Manager manager = managerService.findById(id);
        model.addAttribute("manage",manager);
        return "editRole";
    }

    /**
     * 修改角色
     * @param manager
     * @return
     */

    @RequestMapping("editRole")
    public String editRole(Manager manager){
        Role role = roleService.findByRoleName(manager.getRoleType());
        manager.setRoleId(role.getRoleId());
        managerService.updateRole(manager);
        return "redirect:/manager/findAllManager";
    }
    /**
     * ShiroLogin
     */
    @ResponseBody
    @RequestMapping("shiroLogin")
    public String shiroLogin(Manager manager,HttpSession session ){
        Subject currentUser = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(manager.getManagerName(),manager.getManagerPassword());
        try {
            currentUser.login(token);//会触发com.itclj.common.shiro.UserRealm的doGetAuthenticationInfo方法
            session.setAttribute("manager",manager);
        } catch (UnknownAccountException e) {
          ///  model.addAttribute("msg","用户不存在");
            return "UnknownAccount";
        }catch (IncorrectCredentialsException e){
         //   model.addAttribute("msg","密码错误");
            return "IncorrectPassword";
        }

    return "ok";
    }

}
